package org.dzms.dao;

import java.util.List;

import org.dzms.commons.DataHandler;
import org.dzms.commons.IPage;
import org.dzms.commons.PageHandler;
import org.dzms.entity.Userinfos;

public class UserinfosDAO implements IPage {
	/**
	 * ========================================================
	 * 												用户信息查询
	 * ========================================================
	 */
	//查询所有用户信息
	public List<Userinfos> findUsers() {
		String sql = "select * from userinfos";
		return DataHandler.query(sql, null, Userinfos.class);
	}

	//按条件查询（登录的方法-通过手机）
	public List<Userinfos> findUsersBy(Userinfos userinfos) {
		String sql = "select * from userinfos where telephone = ?";
		return DataHandler.query(sql, new Object[]{userinfos.getTelephone()}, Userinfos.class);
	}
	
	//按条件查询（注册的方法，判断用户名是否冲重复）
		public List<Userinfos> findUsersByUsername(Userinfos userinfos) {
			String sql = "select * from userinfos where username = ?";
			return DataHandler.query(sql, new Object[]{userinfos.getUsername()}, Userinfos.class);
		}
	//修改积分值
	public int updateIntegral(int userid){
		String sql="update userinfos set integral=integral+10 where userid=?";
		return DataHandler.update(sql, new Object[]{userid});
	}
	
	//按条件查询（通过Id拿username）
	public List<Userinfos> findUsersByID(int userid){
		String sql = "select * from userinfos where userid="+userid;
		return DataHandler.query(sql, null, Userinfos.class);
	}
	
	//保存用户信息
	public void saveUsers(Userinfos us) {
		String sql = "insert into userinfos(username, usersex, telephone ,password, "
				+ "userpic, integral, introducemyself, postcode) values(?,?,?,?,?,?,?,?)";
		DataHandler.update(sql, new Object[]{us.getUsername(),us.getUsersex(), us.getTelephone(), 
				us.getPassword(),us.getUserpic(), us.getIntegral(),
				us.getIntroducemyself(), us.getPostcode()});
	}
	
	//删除用户信息
	public void deleteUsers(int userid) { 
		String sql = "delete from userinfos where userid=?";
		DataHandler.update(sql, new Object[]{userid});
	}
	//更新用户信息
	public void updateUsers(Userinfos us) {
		String sql = "update userinfos set username=?, usersex=?, telephone=?"
				+ ", password=?, userpic=?, integral=?, introducemyself=?"
				+ ", postcode=? where userid=?";
		DataHandler.update(sql, new Object[]{us.getUsername(), us.getUsersex(), us.getTelephone(),
				us.getPassword(),us.getUserpic(), us.getIntegral(),
				us.getIntroducemyself(), us.getPostcode(), us.getUserid()});
	}
	
	//更新用户基本信息
		public void updateStandardUsers(Userinfos us) {
			String sql = "update userinfos set username=?, usersex=?"
					+ ", alwaddress=?, introducemyself=?"
					+ ", actaddress=? where userid=?";
			DataHandler.update(sql, new Object[]{us.getUsername(), us.getUsersex(),
					us.getAlwaddress(),
					us.getIntroducemyself(),us.getActaddress(), us.getUserid()});
		}
	

	public <E> List<E> findCurrentPageData(PageHandler handler) {
		return null;
	}

	public int findCountPage(PageHandler handler) {
		return 0;
	}

	public <E> List<E> foods(String a, PageHandler handler) {
		// TODO Auto-generated method stub
		return null;
	}

	public <E> List<E> findCurrentPageDatadesc(PageHandler handler) {
		// TODO Auto-generated method stub
		return null;
	}

}
